// Source File Name:   BINImpl.java

package com.sunwayhorizo.health.hl7.datatypes.common;

import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.datatypes.ANY;
import com.sunwayhorizo.health.hl7.datatypes.BIN;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.BN;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.INT;
import com.sunwayhorizo.health.hl7.datatypes.LIST_BN;
import com.sunwayhorizo.health.hl7.datatypes.ST;
import com.sunwayhorizo.health.hl7.datatypes.TYPE;

import java.util.Iterator;



// Referenced classes of package oracle.apps.ctb.hl7.types.common:
//            ANYImpl, BLImpl, CSImpl, TYPEImpl

public class BINImpl extends ANYImpl
    implements BIN
{

    public static final String RCS_ID = "$Header: BINImpl.java 115.25 2005/09/16 09:23:05 amchanda noship $";
    protected byte _value[];
    private static final BIN nullNI = new BINImpl(CSImpl.valueOf("NI"));
    private static final BIN nullOTH = new BINImpl(CSImpl.valueOf("OTH"));
    private static final BIN nullNINF = new BINImpl(CSImpl.valueOf("NINF"));
    private static final BIN nullPINF = new BINImpl(CSImpl.valueOf("PINF"));
    private static final BIN nullUNK = new BINImpl(CSImpl.valueOf("UNK"));
    private static final BIN nullASKU = new BINImpl(CSImpl.valueOf("ASKU"));
    private static final BIN nullNAV = new BINImpl(CSImpl.valueOf("NAV"));
    private static final BIN nullMSK = new BINImpl(CSImpl.valueOf("MSK"));
    private static final BIN nullNA = new BINImpl(CSImpl.valueOf("NA"));
    private static final BIN nullNASK = new BINImpl(CSImpl.valueOf("NASK"));

    public static BIN valueOf(byte abyte0[])
    {
        if(abyte0 == null || abyte0.length == 0)
            throw ANYImpl.newNullException("BIN", "valueOf", "value");
        else
            return new BINImpl(abyte0);
    }
    /**
     * 得到二进制返回值
     * ·@author jinzw
     *  @data 2010-11-09
     */
    public byte[] byteArray()
    {
    	return _value;
    }
    public static BIN nullBIN()
    {
        return new BINImpl(((CS) (null)));
    }

    protected BINImpl(byte abyte0[])
    {
        _value = abyte0;
    }

    protected BINImpl(CS cs)
    {
        super(true, cs);
        _value = null;
    }

    public BL contains(BN bn)
    {
        throw ANYImpl.newUnsupportedException("BIN", "contains");
    }

    public BN head()
    {
        throw ANYImpl.newUnsupportedException("BIN", "head");
    }

    public BL isEmpty()
    {
        if(isNull().isTrue())
            return BLImpl.TRUE;
        else
            return BLImpl.FALSE;
    }

    public BN item(INT int1)
    {
        throw ANYImpl.newUnsupportedException("BIN", "item");
    }

    public CS encoding()
    {
        throw ANYImpl.newUnsupportedException("BIN", "encoding");
    }

    public ST literal()
    {
        throw ANYImpl.newUnsupportedException("BIN", "literal");
    }

    public INT length()
    {
        throw ANYImpl.newUnsupportedException("BIN", "length");
    }

    public Iterator listIterator()
    {
        throw ANYImpl.newUnsupportedException("BIN", "listIterator");
    }

    public BL notEmpty()
    {
        if(!isNull().booleanValue())
            return BLImpl.TRUE;
        else
            return BLImpl.FALSE;
    }

    public LIST_BN tail()
    {
        throw ANYImpl.newUnsupportedException("BIN", "tail");
    }

    public TYPE dataType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.BIN;
    }

    public BL equal(ANY any)
    {
        if(!(any instanceof BIN))
            return BLImpl.FALSE;
        if(isNull().or(any.isNull()).isTrue())
        {
            return BLImpl.FALSE;
        } else
        {
            BIN bin = (BIN)any;
            return BLImpl.valueOf(stringValue().equals(bin.stringValue()));
        }
    }

    public int hashCode()
    {
        int i = 17;
        if(isNull().isFalse())
        {
            for(int j = 0; j < _value.length; j++)
                i += _value[j];

        }
        return i;
    }

    public String stringValue()
    {
        return new String(_value);
    }

//    public XMLNode[] writeXML(XMLDocument xmldocument, String s)
//        throws Hl7Exception
//    {
//        try
//        {
//            XMLElement xmlelement = (XMLElement)xmldocument.createElement(s);
//            String s1 = stringValue();
//            if(s1 != null)
//            {
//                org.w3c.dom.Text text = xmldocument.createTextNode(s1);
//                xmlelement.appendChild(text);
//            }
//            return (new XMLNode[] {
//                xmlelement
//            });
//        }
//        catch(Exception exception)
//        {
//            throw new Hl7Exception(exception);
//        }
//    }

    public static BIN nullBIN(CS cs)
    {
        String s = null;
        if(cs == null || cs.literal().stringValue() == null || cs.isNull().isTrue())
            s = "NI";
        else
            s = cs.literal().stringValue();
        if(s.equals("NI"))
            return nullNI;
        if(s.equals("OTH"))
            return nullOTH;
        if(s.equals("NINF"))
            return nullNINF;
        if(s.equals("PINF"))
            return nullPINF;
        if(s.equals("UNK"))
            return nullUNK;
        if(s.equals("ASKU"))
            return nullASKU;
        if(s.equals("NAV"))
            return nullNAV;
        if(s.equals("MSK"))
            return nullMSK;
        if(s.equals("NA"))
            return nullNA;
        if(s.equals("NASK"))
            return nullNASK;
        else
            return nullNI;
    }

}
